package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.common.Constants;
import com.api.syh56.shengyunhui.entity.Quote;
import com.api.syh56.shengyunhui.entity.Routes;
import com.api.syh56.shengyunhui.entity.User;
import com.api.syh56.shengyunhui.entity.Waybill;
import com.api.syh56.shengyunhui.entity.adminEntity.AdminContact;
import com.api.syh56.shengyunhui.entity.adminEntity.AdminUser;
import com.api.syh56.shengyunhui.entity.adminEntity.WaybillRoute;
import com.api.syh56.shengyunhui.mapper.WaybillRouteMapper;
import com.api.syh56.shengyunhui.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class WaybillRouteServiceImpl extends ServiceImpl<WaybillRouteMapper, WaybillRoute> implements WaybillRouteService {
    @Autowired
    private WayBillService wayBillService;

    @Autowired
    private RoutesService routesService;

    @Autowired
    private UserService userService;

    @Autowired
    private QuoteService quoteService;

    @Autowired
    private AdminContactService adminContactService;

    @Autowired
    private AdminSupService adminSupService;

    @Autowired
    private WaybillRouteMapper waybillRouteMapper;

    @Override
    public void insetByWid(Long wid) {
        Waybill waybill = wayBillService.getById(wid);
        Long oriAdCode = waybill.getOriAdCode();
        Long dstAdCode = waybill.getDstAdCode();
        //获取匹配专线
        QueryWrapper<Routes> routesQueryWrapper = new QueryWrapper<>();
        routesQueryWrapper.lambda()
                .eq(Routes::getOriAdCode,oriAdCode)
                .eq(Routes::getDstAdCode,dstAdCode);
        List<Routes> list = routesService.list(routesQueryWrapper);
        System.out.println("size:" + list.size());
        for(Routes routes : list) {
            Long uid = routes.getUid();
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.lambda()
                    .eq(User::getId,uid)
                    .eq(User::getAuthStatus, Constants.Role.DRIVER);
            User user = userService.getOne(userQueryWrapper);
            //获取匹配专线用户信息
            if (user != null) {
                WaybillRoute waybillRoute = new WaybillRoute();
                BeanUtils.copyProperties(user, waybillRoute);
                waybillRoute.setUid(user.getId());
                //查看专线用户是否为运单报价
                waybillRoute.setQuoteStatus(Constants.WaybillQuote.UNQUOTE);
                waybillRoute.setContactStatus(Constants.CONTACT.UN_CONTACT);
                waybillRoute.setWid(wid);
                waybillRouteMapper.insert(waybillRoute);
            }
        }
    }

    @Override
    public void updateQuoteStatus(Long uid, Long wid) {
        UpdateWrapper<WaybillRoute> waybillRouteUpdateWrapper = new UpdateWrapper<>();
        waybillRouteUpdateWrapper.lambda()
                .eq(WaybillRoute::getUid,uid)
                .eq(WaybillRoute::getWid,wid)
                .set(WaybillRoute::getQuoteStatus,Constants.WaybillQuote.IS_QUOTE);
        waybillRouteMapper.update(waybillRouteUpdateWrapper);
    }

    @Override
    public void updateContactStatus(Long uid, Long wid, Long sid) {
        AdminUser adminUser = adminSupService.getById(sid);
        UpdateWrapper<WaybillRoute> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(WaybillRoute::getUid,uid)
                .eq(WaybillRoute::getWid,wid)
                .set(WaybillRoute::getContactStatus,Constants.CONTACT.IS_CONTACT)
                .set(WaybillRoute::getAdminUsername,adminUser.getUsername());
        waybillRouteMapper.update(wrapper);
    }
}
